//到货通知单
$(function(){
	//搜索
	$('.searchBar').click(function(){
		var thisform=$(this).parent('form');
		var goods_name=thisform.find('input[name=goods_name]').val();
		var goods_no=thisform.find('input[name=goods_no]').val();
		$('#booksInfoDatagrid').datagrid('options').pageNumber=1;//设置页码初始值为1
		$('#booksInfoDatagrid').datagrid({ 
			queryParams:{
				goods_name:goods_name,
				goods_no:goods_no
			}
		});
	})
	
	//更新行数据
	function booksInfoUpdateActions(value){
		$.post(booksInfoEditUrl,{
									id			:value.id,
									goods_name	:value.goods_name,
									goods_no	:value.goods_no,
									goods_spec	:value.goods_spec,
									unit1		:value.unit1,
									unit1_count	:value.unit1_count,
									currency	:value.currency,
									country_code:value.country_code,
									source_no	:value.source_no,
									net_weight	:value.net_weight,
									gross_weight:value.gross_weight,
									bar_code	:value.bar_code,
									goods_tax	:value.goods_tax,
									price		:value.price,
									unit2		:value.unit2,
									receive_amount:value.receive_amount
								},function(result){
            if (!result.status){
                $.messager.alert('错误提示',result.message,'error');
            }
        },'json').error(function(data){
        	var info=eval('('+data.responseText+')');
			$.messager.confirm('错误提示',info.message,function(r){
				//$('#authAccessSetDialog').dialog('close');
			});
        });
	};

	var editIndex = undefined;
	function endEditing(){
		if (editIndex == undefined){return true}
		if ($('#booksInfoDatagrid').datagrid('validateRow', editIndex)){
			$('#booksInfoDatagrid').datagrid('endEdit', editIndex);
			editIndex = undefined;
			return true;
		} else {
			return false;
		}
	};
	//双击行编辑
	function onDblClickRow(index){
		if (editIndex != index){
			if (endEditing()){
				$('#booksInfoDatagrid').datagrid('selectRow', index);
				$('#booksInfoDatagrid').datagrid('endEdit', editIndex);
				$('#booksInfoDatagrid').datagrid('beginEdit', index);
				editIndex = index;
			} else {
				$('#booksInfoDatagrid').datagrid('selectRow', editIndex);
			}
		}
	};
	$('#booksInfoDatagrid').datagrid({
		//url:getarrivalUrl+"?randnum="+Math.floor(Math.random()*1000000),
		url:getbooksInfoUrl,
		title:'商品管理',
		fitColumns:true,
		rownumbers:true,
		pageSize:25,
		pageNumber:1,
		pageList:[5,10,15,20,25,30,40,50],
		columns:[[
		{field:'id',title:'id',checkbox:true},
		{field:'goods_no',title:'商品编码',sortable:true,
				editor: {
					type: 'numberbox',  
					options: { 
						required: true,  
						missingMessage: '不能为空',
						min:1
					}  
				}
		},
		{field:'goods_name',title:'商品名称',sortable:true,
				editor: {
					type: 'text',  
					options: { 
						required: true,  
						missingMessage: '不能为空',
						min:1
					}  
				}
		},
		{field:'goods_brand',title:'品牌',sortable:true,
				editor: {
					type: 'text',  
					options: { 
						required: true,  
						missingMessage: '不能为空',
						min:1
					}  
				}
		},
		{field:'goods_marque',title:'型号',sortable:true,
				editor: {
					type: 'text',  
					options: { 
						required: true,  
						missingMessage: '不能为空',
						min:1
					}  
				}
		},
		{field:'goods_pic',title:'缩略图',sortable:true,
				formatter:function(val,rowData,rowIndex){
					console.log(val);
					if(val!="" && val!=null){
						return '<img src="'+val+'" style="width:50px;height:50px;cursor:pointer" attr_val="1" class="ss"  />';
					}
			    }
		},
		{field:'price',title:'商品价格',sortable:true,
				editor: {
					type: 'text',  
					options: { 
						required: true,  
						missingMessage: '不能为空',
						min:1
					}  
				}
		},
		{field:'receive_amount',title:'数量',sortable:true,
				editor: {
					type: 'text',  
					options: { 
						required: true,  
						missingMessage: '不能为空',
						min:1
					}  
				}
		},
		{field:'goods_tax',title:'税率(%)',sortable:true,
				editor: {
					type: 'text',  
					options: { 
						required: true,  
						missingMessage: '不能为空',
						min:1
					}  
				}
		},
		{field:'bar_code',title:'条形码',sortable:true,
				editor: {
					type: 'text',  
					options: {
						required: true,  
						missingMessage: '不能为空',
						min:1
					}  
				}
		},
		{field:'source_no',title:'料号',sortable:true,
				editor: {
					type: 'text',  
					options: { 
						required: true,  
						missingMessage: '不能为空',
						min:1
					}  
				}
		},

		{field:'net_weight',title:'净重',sortable:true,
				editor: {
					type: 'text',
					options: { 
						required: true,  
						missingMessage: '不能为空',
						min:1
					}  
				}
		},
		{field:'gross_weight',title:'毛重',sortable:true,
				editor: {
					type: 'text',  
					options: { 
						required: true,  
						missingMessage: '不能为空',
						min:1
					}  
				}
		},
		{field:'add_time',title:'添加时间',sortable:true},
		{field:'update_time',title:'更新时间',sortable:true}
		]],
		queryParams:{
			cate_id:cateId
		},
		onDblClickRow: onDblClickRow,
		onAfterEdit:function(index,row){
			//console.log(row);
					row.editing = false;
					booksInfoUpdateActions(row);
		},
		onLoadError:function(data){
			var info=eval('('+data.responseText+')');
			$.messager.confirm('错误提示',info.message,function(r){
				var tab=$('#Tabs').tabs('getSelected');
				var index=$('#Tabs').tabs('getTabIndex',tab);
				$('#Tabs').tabs('close',index);
			});
		},
		rowStyler:function(index,row){
			if (row.color==0){
				return 'background-color:pink;color:blue;font-weight:bold;';
			}
		},
		pagination:true,
		fit:true,
	});
});
booksInfoObj={
	add:function(url){//新增数据
		$('#booksInfoAddForm').form('submit',{
			url:url,
            onSubmit: function(){
                return $(this).form('validate');
            },
            success: function(result){
            	var result=eval('('+result+')');
                if(!result.status){
                    $.messager.confirm('错误提示',result.message,function(r){
                    	$('#booksInfoAddDialog').dialog('close');  
                    });
                }else{
					$.messager.alert('提示信息',result.message,'info',function(r){
                    	$('#booksInfoAddDialog').dialog('close');       
						$('#booksInfoDatagrid').datagrid('reload'); 
                    });
                }
            },
		});
	},
	save:function(url){//编辑后保存数据
		$('#booksInfoEditForm').form('submit',{
            url: url,
            onSubmit: function(){
                return $(this).form('validate');
            },
            success: function(result){
            	 var result=eval('('+result+')');
                if (!result.status){
                    $.messager.confirm('错误提示',result.message,function(r){
                    	$('#booksInfoEditDialog').dialog('close');
                    });
                }else{
                    $('#booksInfoEditDialog').dialog('close');      
                    $('#booksInfoDatagrid').datagrid('reload');    
                }
            }
        });
	},
	saveBar:function(){//保存数据
		var editIndex = undefined;
		if (editIndex == undefined){endEditing = 1;}
		if ($('#booksInfoDatagrid').datagrid('validateRow', editIndex)){
			$('#booksInfoDatagrid').datagrid('endEdit', editIndex);
			editIndex = undefined;
			endEditing = 1;
		} else {
			endEditing = 0;
		}
		if (endEditing == 1){
			$('#booksInfoDatagrid').datagrid('acceptChanges');
		}
	},
	showAll:function(){//显示默认条件下所有数据
		$('#booksInfoDatagrid').datagrid({ queryParams:''});
	},
	reloadBar:function(){//刷新数据
		$('#booksInfoDatagrid').datagrid('reload');
	},
	addBar:function(){//显示添加框
		$('#booksInfoAddDialog').dialog('open').dialog('setTitle','添加');
		$('#booksInfoAddForm').form('clear');
		$('#unit1').combobox('select',0);
		$('#unit2').combobox('select',0);
		$('#current_type').combobox('select',0);
		$('#countrylist').combobox('select',0);
	},
	editBar:function(){//显示编辑框
		var booksInfoRow=$('#booksInfoDatagrid').datagrid('getSelections');
        if (booksInfoRow.length>1){		            	
            $.messager.alert('提示','一次只能修改一条记录!','info');
        }else if(booksInfoRow.length==1){            	
        	$('#booksInfoEditDialog').dialog('open').dialog('setTitle','编辑');
            $('#booksInfoEditForm').form('load',booksInfoRow[0]);
        }else{
			$.messager.alert('推送提示','请选择列表数据','error');
		}
	},
	removeBar:function(){//删除数据
		var booksInfoRow=$('#booksInfoDatagrid').datagrid('getSelections');
        if (booksInfoRow.length>0){
        	$.messager.confirm('删除提示','真的要删除这'+booksInfoRow.length+'个信息吗?',function(r){
                if (r){
                	var ids=[];
                	for(var i=0;i<booksInfoRow.length;i++){
                		ids.push(booksInfoRow[i].id);
                	}
                	var id=ids.join(',');
                    $.post(booksInfoDelUrl,{id:id},function(result){
                        if (result.status){
							$.messager.alert('提示信息',result.message,'info',function(r){
								$('#booksInfoAddDialog').dialog('close');       
								$('#booksInfoDatagrid').datagrid('reload'); 
							});
                        }else{
                            $.messager.alert('错误提示',result.message,'error');
                        }
                    },'json').error(function(data){
                    	var info=eval('('+data.responseText+')');
						$.messager.confirm('错误提示',info.message,function(r){
							//$('#authAccessSetDialog').dialog('close');
						});
                    });	
                }
        	});
		}else{
			$.messager.alert('推送提示','请选择列表数据','error');
		}
	}
}